package inner_sort

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/25 23:54
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 快速排序-单向快排
 * */
type QuickSortSimplex struct {
	FromHighToLow bool // true表示从高到低排序,默认 false 表示从低到高排序
}

func NewQuickSortSimplex(options ...C.Options) *BucketSort {
	opt := C.NewOptions(options...)
	return &BucketSort{
		FromHighToLow: opt.FromHighToLow,
	}
}

func (m *QuickSortSimplex) SortMethod(array []int) {
	if len(array) < 2 {
		return
	}
	m.sort(array, 0, len(array)-1)
}
func (m *QuickSortSimplex) sort(array []int, left, right int) {
	if left >= right {
		return
	}
	cur, l := left, left+1
	for l <= right {
		if array[l] < array[left] {
			cur++
			array[cur], array[l] = array[l], array[cur]
		}
		l++
	}
	if cur != left {
		array[cur], array[left] = array[left], array[cur]
	}
	m.sort(array, left, cur-1)
	m.sort(array, cur+1, right)
}

func (m *QuickSortSimplex) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.Sort},
		Desc: &C.Desc{
			Name:   "QuickSortSimplex",
			NameCn: "内部排序:快速排序:单向快排",
			ParamsDesc: map[string]string{
				"fromHighToLow": "true表示从高到低排序, 默认 false 表示从低到高排序",
			},
			Example: map[int]string{
				1: "输入[1,3,2],跳用SortMethod方法进行排序后，输出[1,2,3]",
			},
		},
	}
}